package com.heima.search.test;

import com.heima.article.client.ArticleFeign;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.search.service.UserSearchServiceImpl;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.CollectionUtils;

import java.util.List;

@SpringBootTest
@RunWith(SpringRunner.class)
public class Db2Es {
    @Autowired
    private ArticleFeign articleFeign;
    @Autowired
    private UserSearchServiceImpl userSearchService;
    /**
     * 远程调用article
     * 查询文章数据
     * 导入Es
     */
    @Test
    public void db2Es(){
        int page=1,size=3;
        while(true) {
//        远程调用article,分页查询，返回文章数据
            List<ArticleDto> articleDtoList = articleFeign.findByPage(page, size);
            if(CollectionUtils.isEmpty(articleDtoList)){
//                已经没有数据，结束
                break;
            }
            System.out.println("==================page="+page);
//        调用es的api，写入文档数据
            userSearchService.importArticle(articleDtoList);
            if(articleDtoList.size()<size){
                break;
            }
            page++;
        }
    }
}
